using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.OleDb;

using Barbar.MapReport.Common;
using Barbar.MapReport.Configuration;
using Barbar.MapReport.GeoCoding;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class DataProvider {
  private static DataProvider _Instance;
  private static object _SyncRoot = new object();

  private decimal m_CampaignCost;
  private ReadOnlyCollection<Contact> m_Contacts;

  private DataProvider() {
    string path = HttpContext.Current.Server.MapPath("~/App_Data/CampaignData.xml");
    XmlSerializer serializer = new XmlSerializer(typeof(CampaignData));
    CampaignData data = null;
    using (XmlReader reader = XmlReader.Create(path)) {
      data = (CampaignData)serializer.Deserialize(reader);
    }

    m_CampaignCost = data.CampaignCost;
    data.Contacts.EnsureGps();
    m_Contacts = new ReadOnlyCollection<Contact>(data.Contacts);
  }

  public IEnumerable<Contact> GetContactByResponseFlag(int responseFlag) {
    foreach (Contact contact in m_Contacts)
      if ((contact.ResponseType.BitFlag & responseFlag) != 0)
        yield return contact;
  }

  public ReadOnlyCollection<Contact> Contacts {
    get { return m_Contacts; }
  }

  public decimal CampaignCost {
    get { return m_CampaignCost; }
  }

  public static DataProvider Instance {
    get {
      lock (_SyncRoot) {
        if (_Instance == null)
          _Instance = new DataProvider();
        return _Instance;
      }
    }
  }
}
